package com.wan.data.mysql;

import cn.hutool.db.DbUtil;
import cn.hutool.setting.Setting;
import com.wan.IKit;
import com.wan.data.mysql.exception.DBInitializeException;
import com.wan.data.mysql.factory.CustomHikariCpFactory;

public class MysqlKit extends IKit {

    @Override
    public void init() {
        String[] strings = groupSetting.getStrings(DbConstant.ConfVar.DB_PATH_VAR);
        if(strings == null){
            throw new DBInitializeException("未数据源配置文件!!");
        }
        try {
            DataSourceManager.load(strings);
        } catch (Exception e) {
            throw new DBInitializeException(e);
        }
    }

    @Override
    public void injectConf(Setting setting) {
        super.injectConf(setting);
        setConfig();
    }

    private void setConfig(){
        Boolean create = groupSetting.getBool(DbConstant.ConfVar.DB_AUTO_CREATE, false);
        Boolean fix = groupSetting.getBool(DbConstant.ConfVar.DB_AUTO_FIX, false);
        DataSourceManager.setAutoCreate(create);
        DataSourceManager.setAutoFix(fix);
        DbUtil.setShowSqlGlobal(groupSetting);
        //连接池类型 -> 可以做到配置中指定
        DataSourceManager.setDataSourceFactory(new CustomHikariCpFactory());
    }

    @Override
    public void shutdown() {
    }
}
